// Copyright (C) 2016 All rights reserved.
// Author: liufeng11@letv.com (Liu Feng)
// Description:
#ifndef _HAARFONTFEATUREEXTRACTOR_H
#define _HAARFONTFEATUREEXTRACTOR_H

#include "FontFeatureExtractor.h"
#include <cmath>

class HaarFontFeatureExtractor : public FontFeatureExtractor {
public:
  virtual void ExtractFeature(const unsigned char* data, std::vector<double>* feature);
private:
  template<class T>
  inline T& P(T* data, int i, int j) {
    return data[i*COLS+j];
  }

  template<class T>
  inline T P(const T* data, double i, double j) {
    // bilinear interpolation
    if (i < 0 || j < 0) return 0;
    if (i >= ROWS-1 && j < COLS) return data[(ROWS-1)*COLS+(int)j];
    if (i < ROWS && j >= COLS-1) return data[(int)i*COLS+COLS-1];
    if (i >= ROWS-1 && j >= COLS-1) return data[ROWS*COLS-1];
    int x = floor(j);
    int y = floor(i);
    double l = j - x;
    double h = i - y;
    T p11, p12, p21, p22;
    p11 = data[y*COLS+x];
    p12 = data[y*COLS+x+1];
    p21 = data[(y+1)*COLS+x];
    p22 = data[(y+1)*COLS+x+1];
    T pixel = p11*(1-l)*(1-h) + p12*l*(1-h) + p21*(1-l)*h + p22*l*h;

    return pixel; 
  }
  void ExtractHaarFeature(const double* I, int rows, int cols, int scale, std::vector<double>* v);
};

#endif //HAARFONTFEATUREEXTRACTOR_H
